<template>
  <div>
    <bread-crumb
      class="bread-crumb-padding"
      :list="breadCrumb"
      :user="true"
    ></bread-crumb>
    <div class="user-main-box">
      <user-tab-list :list="tablist"></user-tab-list>
      <div style="position: relative">
        <div class="filter-line flx-row-s-c">
          <span class="item-name">登录时间</span>
          <DatePicker
            :editable="false"
            type="daterange"
            split-panels
            placeholder="请选择"
            style="width: 210px"
            @on-change="dateChange"
            :value="time"
          ></DatePicker>
          <div
            class="user-strong-button line-button-size"
            style="margin: 0 10px 0 30px"
            @click="pageChange(1)"
          >
            搜索
          </div>
          <div class="user-normal-button line-button-size" @click="reset">
            重置
          </div>
        </div>
      </div>
    </div>

    <div class="user-main-box pub-m-t-20">
      <div style="position: relative">
        <Spin :withIcon="true" v-if="loading"></Spin>
        <div class="filter-line search-result-line flx-row-s-c">
          <span class="c-g f-14"
            >搜索结果共<span class="c-black">{{
              total != null ? ` ${total} ` : ""
            }}</span
            >条</span
          >

          <div
            class="user-strong-button line-button-size pub-m-l-30"
            @click="reset"
          >
            刷新本页
          </div>
        </div>
        <table class="table-area">
          <thead>
            <tr>
              <th><div class="line-style" style="justify-content: center;min-width: 48px">序号</div></th>
              <th><div class="line-style">登录时间</div></th>
              <th><div class="line-style">登录IP</div></th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in list" :key="index">
              <td>
                <div class="line-style" style="justify-content: center;">
                  <span class="normal-f">{{
                    (page - 1) * pageSize + index + 1
                  }}</span>
                </div>
              </td>
              <td>
                <div class="line-style">
                  <span class="normal-f">{{ item.createdTime }}</span>
                </div>
              </td>
              <td>
                <div class="line-style">
                  <span class="normal-f"
                    >{{ item.ip }}（{{ item.address }})</span
                  >
                </div>
              </td>
            </tr>
          </tbody>
        </table>
        <Empty-State
          v-if="total != null && total == 0"
          content="未搜索到任何内容！"
        ></Empty-State>
        <div class="form-page-bottom-area flx-row-c-c">
          <Page
            :total="total"
            :current="page"
            :page-size="pageSize"
            :page-size-opts="pageSizeOpts"
            @on-change="pageChange"
            @on-page-size-change="pageSizeChange"
            show-elevator
            show-sizer
            show-total
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getLoginLog } from "@/api/user";
export default {
  name: "VipSecurityLog",
  data() {
    return {
      loading: false,
      breadCrumb: [{name: "管理中心"},{ name: "账户管理" }, { name: "登录日志" }],
      tablist: [
        {
          name: "登录日志",
          to: "/user/securitylog",
        },
      ],
      // 表单数据
      time: [],
      startTime: "",
      endTime: "",

      // 分页设置
      total: null,
      pageSizeOpts: [50, 100, 200],
      pageSize: 50,
      page: 1,
      list: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 日期选择改变
    dateChange(date) {
      this.startTime = date[0];
      this.endTime = date[1];
    },
    // 切换pageSize
    pageSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.pageChange(1);
    },
    // 切换页码
    pageChange(page) {
      this.page = page;
      this.getList();
    },
    // 重置
    reset() {
      this.time = [];
      this.startTime = "";
      this.endTime = "";
      this.page = 1;
      this.getList();
    },
    // 获取列表
    async getList() {
      this.loading = true;
      let params = {
        startTime: this.startTime,
        endTime: this.endTime,
        limit: this.pageSize,
        page: this.page,
      };
      let res = await getLoginLog(params);
      if (res.code == 200) {
        this.list = res.data.list;
        this.total = res.data.total;
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
